{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sad_construct import *\n",
    "from sad_read_mnist import get_mnist\n",
    "(x_train,y_train),(x_test,y_test)=get_mnist()\n",
    "x_train=x_train.reshape(-1,1*28*28)\n",
    "x_test=x_test.reshape(-1,1*28*28)\n",
    "mnist_train_data = np.append(x_train, y_train, axis=1)\n",
    "mnist_test_data = np.append(x_test, y_test, axis=1)\n",
    "mnist_shape=(1,28,28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sad_network import NeuralNetwork as Network\n",
    "from sad_layer import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init para\n",
      "epoch 1:\n",
      " Loss:0.453621107 \tAccuracy: 0.9319 [0.97653, 0.97885, 0.92054, 0.91683, 0.94196, 0.87556, 0.94885, 0.92899, 0.92813, 0.89296]\n",
      "epoch 2:\n",
      " Loss:0.217636030 \tAccuracy: 0.9481 [0.98265, 0.97885, 0.95543, 0.9505, 0.92974, 0.91256, 0.96033, 0.93969, 0.92094, 0.94252]\n",
      "epoch 3:\n",
      " Loss:0.164494780 \tAccuracy: 0.9577 [0.98367, 0.9859, 0.96512, 0.95347, 0.95927, 0.92601, 0.96973, 0.94163, 0.95483, 0.93162]\n",
      "epoch 4:\n",
      " Loss:0.132624338 \tAccuracy: 0.9627 [0.98163, 0.9859, 0.96899, 0.97426, 0.97251, 0.92489, 0.96868, 0.95623, 0.94456, 0.94252]\n",
      "epoch 5:\n",
      " Loss:0.110781747 \tAccuracy: 0.9689 [0.9898, 0.98855, 0.97481, 0.96832, 0.97454, 0.94507, 0.97077, 0.9572, 0.96715, 0.94846]\n"
     ]
    }
   ],
   "source": [
    "train_and_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    train_data=mnist_train_data,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    batch_size=16,\n",
    "    epoch_num=5,\n",
    "    pth=\"fc_mnist_b16.npy\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init para\n",
      "epoch 1:\n",
      " Loss:0.574346250 \tAccuracy: 0.9155 [0.98367, 0.98062, 0.87888, 0.92178, 0.94807, 0.85202, 0.92484, 0.90175, 0.87269, 0.8781]\n",
      "epoch 2:\n",
      " Loss:0.277184165 \tAccuracy: 0.9338 [0.98061, 0.98326, 0.91764, 0.92772, 0.94603, 0.90359, 0.94676, 0.91148, 0.90246, 0.9108]\n",
      "epoch 3:\n",
      " Loss:0.225806251 \tAccuracy: 0.9419 [0.98776, 0.98238, 0.93702, 0.94059, 0.94399, 0.89798, 0.94676, 0.91732, 0.93121, 0.92567]\n",
      "epoch 4:\n",
      " Loss:0.192692634 \tAccuracy: 0.947 [0.98673, 0.98502, 0.94186, 0.94554, 0.96232, 0.93274, 0.94363, 0.9358, 0.93326, 0.89792]\n",
      "epoch 5:\n",
      " Loss:0.168631632 \tAccuracy: 0.9539 [0.98673, 0.9859, 0.95349, 0.9495, 0.95112, 0.93386, 0.95407, 0.94844, 0.93943, 0.93062]\n"
     ]
    }
   ],
   "source": [
    "train_and_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    train_data=mnist_train_data,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    batch_size=32,\n",
    "    epoch_num=5,\n",
    "    half_learning_rate_time=4,\n",
    "    pth=\"fc_mnist_b32.npy\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init para\n",
      "epoch 1:\n",
      " Loss:0.346008193 \tAccuracy: 0.9399 [0.97755, 0.963, 0.89632, 0.9297, 0.94501, 0.88341, 0.94572, 0.95136, 0.96407, 0.93657]\n",
      "epoch 2:\n",
      " Loss:0.161294559 \tAccuracy: 0.9617 [0.98367, 0.98502, 0.9564, 0.96238, 0.95112, 0.94058, 0.97286, 0.96595, 0.95791, 0.93657]\n",
      "epoch 3:\n",
      " Loss:0.114932221 \tAccuracy: 0.9672 [0.98776, 0.98238, 0.97578, 0.9495, 0.97149, 0.96861, 0.97599, 0.97471, 0.94148, 0.94252]\n",
      "epoch 4:\n",
      " Loss:0.088613486 \tAccuracy: 0.9727 [0.98776, 0.98943, 0.97674, 0.97723, 0.97862, 0.94843, 0.9666, 0.96595, 0.97228, 0.95937]\n",
      "epoch 5:\n",
      " Loss:0.070898373 \tAccuracy: 0.975 [0.98673, 0.98943, 0.97868, 0.97129, 0.97352, 0.97197, 0.97286, 0.96304, 0.97536, 0.96531]\n"
     ]
    }
   ],
   "source": [
    "train_and_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    train_data=mnist_train_data,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    batch_size=8,\n",
    "    epoch_num=5,\n",
    "    half_learning_rate_time=1,\n",
    "    pth=\"fc_mnist_b8.npy\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init para\n",
      "epoch 1:\n",
      " Loss:2.374894297 \tAccuracy: 0.0789 [0.01122, 0.04581, 0.0, 0.0, 0.08452, 0.00561, 0.00522, 0.0, 0.05647, 0.57284]\n",
      "epoch 2:\n",
      " Loss:2.366335739 \tAccuracy: 0.0831 [0.01429, 0.06167, 0.0, 0.0, 0.10081, 0.00561, 0.00522, 0.0, 0.05852, 0.57582]\n",
      "epoch 3:\n",
      " Loss:2.358004978 \tAccuracy: 0.0861 [0.01531, 0.07489, 0.0, 0.0, 0.10489, 0.00561, 0.00522, 0.0, 0.05955, 0.58474]\n",
      "epoch 4:\n",
      " Loss:2.349892698 \tAccuracy: 0.0892 [0.01837, 0.08987, 0.0, 0.0, 0.111, 0.00673, 0.00522, 0.0, 0.06263, 0.58573]\n",
      "epoch 5:\n",
      " Loss:2.341980675 \tAccuracy: 0.0929 [0.02041, 0.10749, 0.0, 0.0, 0.11609, 0.00673, 0.00835, 0.0, 0.0616, 0.59366]\n",
      "epoch 6:\n",
      " Loss:2.334247622 \tAccuracy: 0.0981 [0.02653, 0.12863, 0.0, 0.00099, 0.12831, 0.00785, 0.01148, 0.0, 0.06263, 0.59762]\n",
      "epoch 7:\n",
      " Loss:2.326688298 \tAccuracy: 0.1025 [0.03061, 0.14714, 0.0, 0.00198, 0.13951, 0.00897, 0.01461, 0.0, 0.06571, 0.59762]\n",
      "epoch 8:\n",
      " Loss:2.319287292 \tAccuracy: 0.1071 [0.03163, 0.16564, 0.0, 0.00198, 0.15682, 0.00897, 0.02192, 0.0, 0.06571, 0.59762]\n",
      "epoch 9:\n",
      " Loss:2.312032335 \tAccuracy: 0.1131 [0.03776, 0.19471, 0.0, 0.00198, 0.17108, 0.00897, 0.02818, 0.0, 0.06674, 0.59762]\n",
      "epoch 10:\n",
      " Loss:2.304910580 \tAccuracy: 0.1185 [0.0449, 0.22203, 0.0, 0.00198, 0.18126, 0.00897, 0.03653, 0.0, 0.06468, 0.59762]\n",
      "epoch 11:\n",
      " Loss:2.297914428 \tAccuracy: 0.123 [0.04898, 0.24758, 0.0, 0.00198, 0.18839, 0.00897, 0.0428, 0.0, 0.06468, 0.59663]\n",
      "epoch 12:\n",
      " Loss:2.291036300 \tAccuracy: 0.129 [0.05714, 0.27577, 0.0, 0.00396, 0.20061, 0.00897, 0.04906, 0.0, 0.06674, 0.59465]\n",
      "epoch 13:\n",
      " Loss:2.284262873 \tAccuracy: 0.1352 [0.0602, 0.29956, 0.0, 0.00396, 0.21283, 0.01009, 0.05846, 0.0, 0.06879, 0.60258]\n",
      "epoch 14:\n",
      " Loss:2.277584256 \tAccuracy: 0.1407 [0.06429, 0.32511, 0.0, 0.00495, 0.22098, 0.01233, 0.06159, 0.0, 0.06982, 0.60951]\n",
      "epoch 15:\n",
      " Loss:2.270997906 \tAccuracy: 0.1468 [0.07653, 0.34714, 0.0, 0.00594, 0.22912, 0.01233, 0.07098, 0.00097, 0.0729, 0.6115]\n",
      "epoch 16:\n",
      " Loss:2.264494529 \tAccuracy: 0.1545 [0.09184, 0.37709, 0.0, 0.00594, 0.23931, 0.01345, 0.08873, 0.00097, 0.0729, 0.6115]\n",
      "epoch 17:\n",
      " Loss:2.258065776 \tAccuracy: 0.1628 [0.10714, 0.40088, 0.0, 0.00594, 0.24847, 0.01457, 0.11482, 0.00097, 0.07598, 0.61447]\n",
      "epoch 18:\n",
      " Loss:2.251706705 \tAccuracy: 0.1704 [0.11939, 0.42819, 0.00097, 0.00891, 0.25764, 0.0157, 0.13466, 0.00097, 0.07803, 0.61249]\n",
      "epoch 19:\n",
      " Loss:2.245411896 \tAccuracy: 0.1765 [0.12755, 0.44934, 0.00194, 0.0099, 0.26171, 0.01682, 0.14823, 0.00097, 0.08316, 0.61645]\n",
      "epoch 20:\n",
      " Loss:2.239174602 \tAccuracy: 0.1838 [0.13776, 0.47313, 0.00194, 0.01881, 0.26986, 0.01682, 0.16388, 0.00097, 0.08522, 0.61843]\n"
     ]
    }
   ],
   "source": [
    "train_and_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    train_data=mnist_train_data,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    batch_size=60000,\n",
    "    epoch_num=20,\n",
    "    half_learning_rate_time=100,\n",
    "    pth=\"fc_mnist_ball.npy\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init para\n",
      "epoch 1:\n",
      " Loss:0.210584230 \tAccuracy: 0.9658 [0.99184, 0.98678, 0.98934, 0.96733, 0.96334, 0.96413, 0.93528, 0.9572, 0.95483, 0.94351]\n",
      "epoch 2:\n",
      " Loss:0.094823206 \tAccuracy: 0.9604 [0.99286, 0.97181, 0.97093, 0.97426, 0.9002, 0.96861, 0.95825, 0.94747, 0.97331, 0.94549]\n",
      "epoch 3:\n",
      " Loss:0.067755171 \tAccuracy: 0.9736 [0.98878, 0.99119, 0.96802, 0.9802, 0.96945, 0.95852, 0.97495, 0.95525, 0.97536, 0.97126]\n",
      "epoch 4:\n",
      " Loss:0.052916670 \tAccuracy: 0.9765 [0.99184, 0.99648, 0.98643, 0.97822, 0.95825, 0.97646, 0.97704, 0.97763, 0.94251, 0.97621]\n",
      "epoch 5:\n",
      " Loss:0.042108050 \tAccuracy: 0.9787 [0.98571, 0.99295, 0.9845, 0.97327, 0.9776, 0.96973, 0.98539, 0.97471, 0.96612, 0.97423]\n"
     ]
    }
   ],
   "source": [
    "train_and_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    train_data=mnist_train_data,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    batch_size=1,\n",
    "    epoch_num=5,\n",
    "    half_learning_rate_time=1,\n",
    "    pth=\"fc_mnist_b1.npy\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.9689 [0.98979592 0.98854626 0.9748062  0.96831683 0.97454175 0.94506726\n",
      " 0.97077244 0.95719844 0.96714579 0.94846383]\n",
      "Cross Accuracy:\n",
      "[[9.89795918e-01 0.00000000e+00 1.02040816e-03 0.00000000e+00\n",
      "  1.02040816e-03 1.02040816e-03 3.06122449e-03 2.04081633e-03\n",
      "  2.04081633e-03 0.00000000e+00]\n",
      " [0.00000000e+00 9.88546256e-01 2.64317181e-03 8.81057269e-04\n",
      "  0.00000000e+00 8.81057269e-04 2.64317181e-03 0.00000000e+00\n",
      "  4.40528634e-03 0.00000000e+00]\n",
      " [4.84496124e-03 9.68992248e-04 9.74806202e-01 1.93798450e-03\n",
      "  2.90697674e-03 0.00000000e+00 1.93798450e-03 5.81395349e-03\n",
      "  6.78294574e-03 0.00000000e+00]\n",
      " [0.00000000e+00 9.90099010e-04 1.08910891e-02 9.68316832e-01\n",
      "  9.90099010e-04 2.97029703e-03 0.00000000e+00 5.94059406e-03\n",
      "  6.93069307e-03 2.97029703e-03]\n",
      " [1.01832994e-03 0.00000000e+00 4.07331976e-03 0.00000000e+00\n",
      "  9.74541752e-01 0.00000000e+00 5.09164969e-03 2.03665988e-03\n",
      "  2.03665988e-03 1.12016293e-02]\n",
      " [8.96860987e-03 1.12107623e-03 1.12107623e-03 1.79372197e-02\n",
      "  3.36322870e-03 9.45067265e-01 7.84753363e-03 2.24215247e-03\n",
      "  1.00896861e-02 2.24215247e-03]\n",
      " [7.30688935e-03 3.13152401e-03 2.08768267e-03 0.00000000e+00\n",
      "  5.21920668e-03 6.26304802e-03 9.70772443e-01 0.00000000e+00\n",
      "  5.21920668e-03 0.00000000e+00]\n",
      " [9.72762646e-04 9.72762646e-03 1.36186770e-02 3.89105058e-03\n",
      "  2.91828794e-03 0.00000000e+00 0.00000000e+00 9.57198444e-01\n",
      "  9.72762646e-04 1.07003891e-02]\n",
      " [5.13347023e-03 1.02669405e-03 4.10677618e-03 5.13347023e-03\n",
      "  3.08008214e-03 5.13347023e-03 6.16016427e-03 2.05338809e-03\n",
      "  9.67145791e-01 1.02669405e-03]\n",
      " [6.93756194e-03 5.94648167e-03 1.98216056e-03 6.93756194e-03\n",
      "  1.78394450e-02 9.91080278e-04 0.00000000e+00 7.92864222e-03\n",
      "  2.97324083e-03 9.48463826e-01]]\n"
     ]
    }
   ],
   "source": [
    "only_test(\n",
    "    mynetwork=fc_mnist,\n",
    "    test_data=mnist_test_data,\n",
    "    data_shape=mnist_shape,\n",
    "    onehotsize=10,\n",
    "    pth=\"fc_mnist_b16.npy\"\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "face",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
